package org.vacoor.ticket.demo;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.vacoor.ticket.bean.CardType;
import org.vacoor.ticket.bean.Passenger;
import org.vacoor.ticket.bean.SeatType;
import org.vacoor.ticket.bean.Station;
import org.vacoor.ticket.bean.TicketType;
import org.vacoor.ticket.bean.TimeRegionType;
import org.vacoor.ticket.bean.TrainPassType;
import org.vacoor.ticket.bean.TrainType;
import org.vacoor.ticket.exception.ConfigurationException;
import org.vacoor.ticket.util.DateUtils;

public class Configuration {
	Properties props;
	//
	private boolean showConfirm;
	//
	private String username;
	private String password;
	//
	private Date date;
	private Station from;
	private Station arrive;
	//以下在配置文件中为可选配置
	// 车次
	private String trainNo;
	// 是否精确匹配站点名称,eg:上海和上海虹桥是否匹配
	private boolean precision = false;
	// 没有符合条件时,循环查询的次数,抢票时候可以
	private long queryCount;
	// 时间段
	private TimeRegionType region;
	// 列车类型
	private TrainType[] trainType;
	// 列车经过类型:全部,始发,过路
	private TrainPassType passType;
	private List<Passenger> passengers;

	public void configure(String fileName) throws ConfigurationException {
		try{
			props = new Properties();
			props.load(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
			String _show = props.getProperty("showConfirm","true");
			showConfirm = Boolean.parseBoolean(_show);
			username = props.getProperty("username");
			password = props.getProperty("password");
			if("".equals(username) || "".equals(password) || username == null || password == null) {
				throw new ConfigurationException("用户名或密码配置为空");
			}
			//
			String _count = props.getProperty("queryCount", Long.MAX_VALUE + "");
			queryCount = Long.parseLong(_count);
			date = DateUtils.parseDate(props.getProperty("date"),new String[]{"yyyy-MM-dd"});
			from = Station.getInstance(props.getProperty("from"));
			arrive = Station.getInstance(props.getProperty("arrive"));
			//以下为可选
			trainNo = props.getProperty("trainNo", "");
			precision = Boolean.parseBoolean(props.getProperty("precision", "true"));
			
			/*
			 * 1 	00:00--06:00  
			 * 2 	06:00--12:00 
			 * 4 	12:00--18:00 
			 * 8 	18:00--24:00 
			 * 16	00:00--24:00  
			 */
			int _region = Integer.parseInt(props.getProperty("region","16"));
			switch(_region) {
			case 1:
				region = TimeRegionType.ZERO_SIX;
				break;
			case 2:
				region = TimeRegionType.SIX_TWELVE;
				break;
			case 4:
				region = TimeRegionType.TWELVE_EIGHTEEN;
				break;
			case 8:
				region = TimeRegionType.EIGHTEEN_TWENTYFOUR;
				break;
			default:
				region = TimeRegionType.ALL;
			}
			/*
			 * D-动车(包括动车),K-快车,T-特快,Z-直达,O-其他,多个以","隔开
			 */
			String _trainType = props.getProperty("trainType", "D,K,T,Z,O").toUpperCase();
			String[] s = _trainType.split(",");
			trainType = new TrainType[s.length];
			for(int i=0;i<s.length;i++) {
				trainType[i] = TrainType.valueOf(s[i]);
			}
			String _passType = props.getProperty("passType", "ALL");
			passType = TrainPassType.valueOf(_passType);
			//
			SeatType seatType = null;
			Integer _seatType = Integer.parseInt(props.getProperty("seatType"));
			/*
			 	1 硬座
				2 软座
				3 硬卧
				4 软卧
				5 高级软卧
				6 二等座
				7 一等座
				8 特等座
				9 商务座
			 */
			switch (_seatType) {
			case 1:
				seatType = SeatType.HARD_SEAT;
				break;
			case 2:
				seatType = SeatType.SOFT_SEAT;
				break;
			case 3:
				seatType = SeatType.HARD_SLEEPER;
				break;
			case 4:
				seatType = SeatType.SOFT_SLEEPER;
				break;
			case 5:
				seatType = SeatType.VAG_SLEEPER;
				break;
			case 6:
				seatType = SeatType.TWO_SEAT;
				break;
			case 7:
				seatType = SeatType.ONE_SEAT;
				break;
			case 8:
				seatType = SeatType.BEST_SEAT;
				break;
			case 9:
				seatType = SeatType.BUSS_SEAT;
				break;
			default:
				throw new ConfigurationException("座席信息设置有误");
			}
//			seatType = 

			passengers = new ArrayList<Passenger>();
			String name = null;
			int i = 1;
			do {
				String prefix = "passenger_" + i +"_";
				name = props.getProperty(prefix + "name");
				if(name == null || "".equals(name)) {
					break;
				}
				TicketType ticketType = TicketType.valueOf(props.getProperty(prefix + "ticketType"));
				CardType cardType = CardType.valueOf(props.getProperty(prefix + "cardType"));
				String cardNo = props.getProperty(prefix + "cardNo");
				if(cardNo == null || "".equals(cardNo)) {
					break;
				}
				//电话可以为""
				Passenger p = new Passenger(seatType, ticketType, cardType, cardNo, name, "",true);
				passengers.add(p);
				i++;
			}while(i<6 && name != null && !"".equals(name));
			
			if(passengers.size()<1) {
				throw new ConfigurationException("配置文件中没有找到乘客信息");
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new ConfigurationException(e.getMessage());
		}
	}

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}

	public boolean isShowConfirm() {
		return showConfirm;
	}

	public void setShowConfirm(boolean showConfirm) {
		this.showConfirm = showConfirm;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Station getFrom() {
		return from;
	}

	public void setFrom(Station from) {
		this.from = from;
	}

	public Station getArrive() {
		return arrive;
	}

	public void setArrive(Station arrive) {
		this.arrive = arrive;
	}

	public long getQueryCount() {
		return queryCount;
	}

	public void setQueryCount(long queryCount) {
		this.queryCount = queryCount;
	}

	public boolean isPrecision() {
		return precision;
	}

	public void setPrecision(boolean precision) {
		this.precision = precision;
	}

	public TimeRegionType getRegion() {
		return region;
	}

	public void setRegion(TimeRegionType region) {
		this.region = region;
	}

	public TrainPassType getPassType() {
		return passType;
	}

	public void setPassType(TrainPassType passType) {
		this.passType = passType;
	}

	public TrainType[] getTrainType() {
		return trainType;
	}

	public void setTrainType(TrainType[] trainType) {
		this.trainType = trainType;
	}

	public String getTrainNo() {
		return trainNo;
	}

	public void setTrainNo(String trainNo) {
		this.trainNo = trainNo;
	}

	public List<Passenger> getPassengers() {
		return passengers;
	}

	public void setPassengers(List<Passenger> passengers) {
		this.passengers = passengers;
	}
}
